package com.zhuxulin.java.email.mina;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.logging.Logger;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoService;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaServer {
//	private static Logger logger = Logger.getLogger(MinaServer.class);
	private static final String HOST = "127.0.0.1";
	private static final String CODEC = "UTF-8";
	private static final int PORT = 8866;
	private static final int READ_BUFFERED_SIZE = 2048;
	private SocketAddress address = null;
	private IoAcceptor acceptor = null;
	/** idel time*/
	private static final int IDLE_TIME=10;
	public MinaServer() {
		address = new InetSocketAddress(HOST, PORT);
		acceptor = new NioSocketAcceptor();
		acceptor.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(
						new TextLineCodecFactory(
								Charset.forName(CODEC), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue())
						)
				);
		acceptor.getSessionConfig().setReadBufferSize(READ_BUFFERED_SIZE);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDLE_TIME);
		acceptor.setHandler(new ServerHanlder());
	}
	public void start() throws IOException{
		acceptor.bind(address);
	}
	public void sendMsg(){
	}
}
